package se.lth.student.axisandroidcam.client;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;

import javax.annotation.Resource;

import se.lth.cs.fakecamera.Axis211A;
import se.lth.student.axisandroidcam.server.CameraThread;
import se.lth.student.axisandroidcam.server.Server;
import se.lth.student.axisandroidcam.server.ServerOut;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

public class AxisAndroidCamera extends Activity {

	private CameraAggregator imgAdapter;
	private GridView gridView;
	private Axis211A fakeCamera;
	
	public static int PORT;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imgAdapter = new CameraAggregator(this);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(imgAdapter);
		PORT = getResources().getInteger(R.integer.PORT);
		
		fakeCamera = new Axis211A();
		fakeCamera.connect();
		
		/*
		 * temp-booting up server
		 */
		String[] args = new String[1];
		args[0] = "1";
		Server.main(args);
		
		System.out.println("Wait");
		try {
			Thread.sleep(8000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Done!");
		
		byte[] picture = new byte[Axis211A.IMAGE_BUFFER_SIZE];
		int pictureLength = picture.length;
		
		fakeCamera.getJPEG(picture, 0);
		imgAdapter.addImage(picture, pictureLength,0);
		fakeCamera.getJPEG(picture, 0);
		imgAdapter.addImage(picture, pictureLength,1);
		fakeCamera.getJPEG(picture, 0);
		imgAdapter.addImage(picture, pictureLength,2);
		fakeCamera.getJPEG(picture, 0);
		imgAdapter.addImage(picture, pictureLength,2);
		
		/*
		Camera cam1 = new Camera(1, Camera.FAKE_CAMERA);
		ClientIn clientIn = new ClientIn(cam1, "localhost");
		clientIn.start();
		*/
	}

	
    @Override
    protected void onPause() {
            super.onPause();
            Toolbox.printDebug("onPause()");
    }

    @Override
    public void onLowMemory(){
            super.onLowMemory();
            Toolbox.printDebug("onLowMemory()");
    }
    
    @Override
    protected void onStop(){
            super.onStop();
            Toolbox.printDebug("onDestroy()");
            super.onDestroy();
            Toolbox.printDebug("onStop()");
    }

    @Override
    protected void onDestroy() {
            super.onDestroy();
            Toolbox.printDebug("onDestroy()");
            System.exit(0);
    }
}
